Welcome to “Request and Response Objects.”
After watching this video, you will be able to: Explain the Flask Request Object, and explain the Flask Response object.
Let’s learn how to customize routes in Flask. You define the path using the route decorator.
The @app.route decorator defaults to the GET method. Clients can only send GET requests
to the specified path. You can pass in a second argument of methods
to control what HTTP methods the path responds to.
For example, look at the following two methods that are the same.
The GET method is implicit in this code. While this code explicitly specifies the GET
method. Here is another example.
The "/health" URL will respond to GET and POST requests. If the request is GET, the
code outputs the status of ok and the method of GET. If the request is POST, the code outputs
the status of ok and the method of POST. This is the output from the GET curl command.
While this is the output from the POST curl command.
All HTTP calls to Flask contain the request object created from the Flask. Request class.
When a client requests a resource from the Flask server, it is handled by the @app.route
decorator. You can examine and explore the request object in the same method.
The following information is available in the request object:
The address of the server in the form of a tuple, host, port.
The headers sent with the request. And the URL that is the resource asked by
request. access_route that lists all the IP addresses
for requests that are forwarded multiple times. full_path that represents the complete path
of the request, including any query string. is_secure is true if a client makes a request
using HTTPS or WSS protocol. is_JSON is true if the request contains JSON
data. And the Cookies dictionary contains any cookies
sent with the request. In addition, you can access the following
data from the header: Cache-Control: holds information on how to
cache in browsers. Accept: informs the browser what kind of content
type is understood by the client . Accept-Encoding: indicates the code content.
User-Agent: identifies the client, application, operating system, or version.
Accept-Language: requests for a specific language and locale.
And Host: specifies the host and port number of the requested server.
You can replace the request object with a custom request object, which is usually optional
as the Flask Request class attributes and methods are enough.
Let's now look at some actual values printed on the server when a client makes a request.
In this case, the client is the CURL command from the terminal.
The request server is 127.0.0.1 or localhost at port 5000.
You next print out some headers: The host is the full path of localhost: 5000.
The user-agent is curl version 7.79.1. And the client has requested the content type
of application/JSON. Let's look at some more attributes from
the request object. The URL requested is http://localhost:5000.
The access route list contains the single value of 127.0.0.1.
The full path for the request is the home path indicated by a single forward slash.
is_JSON is false as we are not sending any data with the GET request.
is_secure is false as the URL is HTTP and not HTTPS .
And the length of the cookies dictionary is zero.
There are multiple ways to get information from the Request Object.
Use the get_data to access data from the POST request as bytes.
You are then responsible for parsing this data.
You can also use the get_JSON() method to get data parsed as JSON from the post request.
Flask also provides more focused methods to get exact information from the request, so
you don't have to parse the data into a specific type.
args will give you query parameters as a dictionary.
JSON will parse the data into a dictionary. files will provide you user uploaded files.
form contains all values posted in a form submission.
And values combine the args data with the form data.
Now that you know what the request object looks like and the methods to get data from
the request parameters and body, let's see how to extract specific values from this data.
The return types for the methods you have seen so far are either a MultiDict, ImmutableMultiDict,
or a CombinedMultiDict. All these data structures behave like a python
dictionary, and you can use indexing or the GET method to extract values.
For the given URL, you would like to extract the query parameters of "capstone" and "rating."
First, you import Flask and the request modules in your method.
You extract the value for the course argument by using indexing.
Then, you extract the value for the rating argument by using the GET method.
The GET method returns a value of None if the argument is not present, whereas the indexing
method returns an error and stops the server with a "400 Bad Request."
The message shows the request in the browser with a course of capstone and a rating of
10. Flask will provide a response object just
like it provides a request object. You can use the response object to send custom
attributes and headers back to the client. Some common response attributes include
the following: status_code indicates the success or failure of the request.
the headers give more information about the response.
content_type shows the media type of the requested resource.
content_length is the size of the response message body.
content_encoding indicates any encoding applied to the response, so the client knows how to
decode the data. mime-type sets the media type of the response.
And expires contains the date or time after which the response is considered expired.
And here are some standard methods on response objects. set_cookie sets a browser cookie
on the client. delete_cookie deletes a cookie on the client.
Let's learn how Flask works with the response object using the different methods:
A Response object with a status_code of 200 and a mime-type of HTML is automatically created
for you when you return data from the @route method.
JSONify also creates a Response object automatically. You can use make_response to create a custom
response. Flask provides a special redirect method to
return a 302 status-code and redirect the client to another URL.
Finally, Flask provides an abort method to return a response with an error condition.
In this video, you learned that: Flask provides a Request and a Response object
for each client call. You can get additional information from the
Flask Request, like headers. You can parse the Request Object to get query
parameters, body, and other arguments. You can set status on Response objects before
sending a response back to the client.